package cn.xenosp.server.web.controller;

import cn.xenosp.server.common.Result;
import cn.xenosp.server.common.annotation.LogInfo;
import cn.xenosp.server.module.activiti.entity.ActivitiModel;
import cn.xenosp.server.module.activiti.entity.Modeler;
import cn.xenosp.server.module.activiti.service.ModelerService;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Objects;

/**
 * 项目名： service-web
 * 包路径： cn.xenosp.server.web.controller
 * 作者：   TongHui
 * 创建时间: 2025-03-12 17:43
 * 描述: TODO
 * 版本: 1.0
 */
@RestController
@RequestMapping("${app.config.api-base-url}/modeler/")
public class ModelerController {
    @Autowired
    private ModelerService modelService;


    @LogInfo(operationModuleType = "流程设计管理",operationName = "获取配置",operationContent = "获取配置")
    @GetMapping("/stencilset")
    public String stencilset(@RequestBody ActivitiModel activitiModel){
        // 这里的接口路径对应 /web/public/activiti-explorer/editor-app/configuration/url-config.js 这个配置文件
        // 后面时间戳我感觉是为了防止前端 get 请求缓存的问题, 加上时间戳取最新的
        // return ACTIVITI.CONFIG.contextRoot + '/editor/stencilset?version=' + Date.now();
        ClassLoader classLoader = this.getClass().getClassLoader();
        try {
            return IOUtils.toString(Objects.requireNonNull(classLoader.getResourceAsStream("static/stencilset-zh.json")), StandardCharsets.UTF_8);
        } catch (IOException e) {
            e.printStackTrace();
            return "获取 stencilset 配置文件失败";
        }
    }
    @LogInfo(operationModuleType = "流程设计管理",operationName = "保存流程设计",operationContent = "保存流程设计")
    @PostMapping("/save")
    public Result<?> saveDesign(@RequestBody Modeler modeler){
        try {
            return Result.OK("成功",modelService.saveDesign(modeler));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error("失败",null);
    }

    @LogInfo(operationModuleType = "流程设计管理",operationName = "获取模型流程设计",operationContent = "获取模型流程设计")
    @PostMapping("/json")
    public Result<?> getModelerJsonById(@RequestBody Modeler modeler){
        try {
            return Result.OK("成功",modelService.getModelerJsonById(modeler.getId()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error("失败",null);
    }
}
